<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>add project</title>
    <style>
        table{border-collapse:collapse;border-spacing:0;border-left:1px solid #888;border-top:1px solid #888;margin: auto}
        th,td{border-right:1px solid #888;border-bottom:1px solid #888;padding:5px 15px;}
        th{font-weight:bold;}
        body{text-align: center}
    </style>
    {% load static %}
    <script type='text/javascript' src="/static/js/jquery.min.js"></script>
    <script type='text/javascript' src="/static/js/user.js"></script>
    <script type="text/javascript" >
        $(document).ready(function(){
            $("#submit").click(function(){
                var pn = $("#projectName").val();
                if(pn == ''){
                    alert("请输入项目名称");
                    return false;
                }
                $("#projectForm").submit();
            });
            $("input[name='changName']").click(function(){
                $(this).parent().parent().find("td:first").find("input").show();
                $(this).parent().parent().find("td:first").find("label").hide();
                $(this).next().show();
                $(this).hide();
            });
            $("input[name='changNameSubmit']").click(function(){
                var bt = $(this);
                changeProjectInfo(bt);
            });
            $("input[name='changeStatus']").click(function(){
                var bt = $(this);
                changeProjectInfo(bt);
            });
            function changeProjectInfo($bt){
                var newName = $bt.parent().parent().find("td:first").find("input").val();
                var projectId = $bt.parent().parent().find("td:first").find("input").attr("id");
                var projectStatus = $bt.parent().prev().find("input").val();
                if($bt.val() != '确定'){
                    if(projectStatus=='0'){
                        projectStatus = 1;
                    }else{
                        projectStatus = 0;
                    }
                }
                var scrf = $("input[name='csrfmiddlewaretoken']").val();
                $.ajax({
                    url:'/changeProjectInfo/',
                    type:'POST',
                    data:{projectName:newName,projectId:projectId,projectStatus:projectStatus,csrfmiddlewaretoken:scrf},
                    success:function(data){
                        if(data=='1'){
                            alert('操作成功');
                            window.location.href="/projectManagePage/";
                        }else{
                            alert('操作失败');
                        }
                    }
                });
            }
            $("input[name='toWorkers']").click(function(){
                var projectId = $(this).parent().parent().find("td:first").find("input").attr("id");
                $("input[name='projectIdData']").val(projectId);
                $("#allocateProjectForm").submit();
            });

            $("input[name='addCase']").click(function(){
                var projectId = $(this).parent().find("input[name='projectId']").val();
                $("#allocateProjectForm").attr("action","/addCaseToProjectPage/");
                $("input[name='projectIdData']").val(projectId);
                $("#allocateProjectForm").submit();
            });

            $("input[name='deleteProject']").click(function(){
                var projectId = $(this).parent().parent().find("td:first").find("input").attr("id");
                var scrf = $("input[name='csrfmiddlewaretoken']").val();
                if(confirm("确定要删除项目吗？")){
                    $.ajax({
                        url:'/deleteProject/',
                        data:{projectId:projectId,csrfmiddlewaretoken:scrf},
                        type: 'POST',
                        success:function(data){
                            if(data=='1'){
                                alert("删除成功");
                                window.location.reload();
                            }else{
                                alert("删除失败");
                                window.location.reload();
                            }
                        }
                    });
                }
            });
        });
    </script>
</head>
<body>
<div style="width: 80%;height:40px;background-color: aqua;margin: 0 auto ">
    <div style="float:left;margin-left: 10px;margin-top: 10px"><a href="#" name="projectManager">项目管理&nbsp;&nbsp;</a>|</div>
    <div style="float:left;margin-left: 10px;margin-top: 10px"><a href="#" name="testCaseManager">测试用例管理&nbsp;&nbsp;</a>|</div>
    <div style="float:left;margin-left: 10px;margin-top: 10px"><a href="#" name="bugCheck">bug情况查看&nbsp;&nbsp;</a>|</div>
    <div style="float:left;margin-left: 10px;margin-top: 10px"><a href="#" name="registerPage">注册新用户</a>|</div>
    <div style="float:left;margin-left: 10px;margin-top: 10px"><a href="#" name="addMachineInfo" target="_blank">添加测试机</a>|</div>
    <div style="float:right;margin-right:10px;margin-top: 10px"><a href="#" name="logout">退出</a></div>
    <div style="float:right;margin-right:10px;margin-top: 10px">登录人：{{ request.session.showName }}&nbsp;&nbsp;</div>
    <form method="post" name="dataForm">
        {% csrf_token %}
        <input type="hidden" name="dataInput" />
    </form>
</div><p></p>
    <form method="post" action="/addProject/" id="projectForm">
        {% csrf_token %}
        <table>
            <tr>
                <td>项目名称:</td>
                <td colspan="2" style="text-align: left;width: 450px"><input id="projectName" name="projectName" style="width:400px"><font color="red">*</font></td>
            </tr>
    </form>
            <tr>
                <td>项目说明:</td>
                <td colspan="2" style="text-align: left;">
                    <textarea id="projectRemark" name="projectRemark" rows="2" cols="80"></textarea>
                </td>
            </tr>
            <tr>
                <td colspan="3"><button id="submit">提交</button></td>
            </tr>
        </table><p>
        <table>
            <tr>
                <td width="180px">项目名称</td>
                <td width="120px">项目状态</td>
                <td width="400px">操作</td>
            </tr>
            {% for project in projects %}
            <tr>
                <td>
                    <input style="display: none" id="{{ project.id }}" value="{{ project.projectName }}" name="pName"/>
                    <label name="pShow">{{ project.projectName }}</label>
                </td>
                <td><input type="hidden" value={{ project.status  }}>{% if project.status == '1'%}<font color="green">已启用</font>{% else %}<font color="red"> 已停用</font>{% endif %}</td>
                <td>
                    <input value="改名" name="changName" type="button"/>
                    <input value="确定" name="changNameSubmit" type="button" style="display: none"/>
                    {% if project.status == '1' %}
                        <input value="停用" name="changeStatus" type="button"/>
                    {% else %}
                        <input value="启用" name="changeStatus" type="button"/>
                    {% endif %}
                    <input type="button" value="删除" name="deleteProject" />
                    <input value="绑定测试用例" name="addCase" type="button"/>
                    <input value="分配到测试人员" name="toWorkers" type="button"/>
                    <input type="hidden" name="projectId" value="{{ project.id }}"/>

                </td>
            </tr>
            {% endfor %}
        </table>

    <form id="allocateProjectForm" name="allocateProjectForm" action="/allocateProjectToWorkers/" method="post">
        <input name="projectIdData" type="hidden"/>
        {% csrf_token %}
    </form>
</body>
</html>